#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 5e5 + 10, INF = 0x3f3f3f3f;

int n, m, root, idx;
int st[N], tt;
bool vis[N];
struct node
{
    int ls, rs;
    int key, pri;
    int siz;
}tr[N];

int newnode(int x)
{
    int p = ++idx;
    tr[p].ls = tr[p].rs = 0;
    tr[p].key = x, tr[p].pri = rand();
    tr[p].siz = 1;
    return p;
}

void pushup(int x) { tr[x].siz = tr[tr[x].ls].siz + tr[tr[x].rs].siz + 1; }

void split(int x, int k, int& l, int& r)
{
    if (x == 0) { l = r = 0; return; }
    if (tr[x].key <= k) {
        l = x;
        split(tr[x].rs, k, tr[x].rs, r);
    } else {
        r = x;
        split(tr[x].ls, k, l, tr[x].ls);
    }
    pushup(x);
}

int merge(int l, int r)
{
    if (l == 0 || r == 0) return l + r;
    if (tr[l].pri > tr[r].pri) {
        tr[l].rs = merge(tr[l].rs, r);
        pushup(l);
        return l;
    } else {
        tr[r].ls = merge(l, tr[r].ls);
        pushup(r);
        return r;
    }
}

void insert(int x)
{
    int l, r, p;
    split(root, x, l, r);split(l, x - 1, l, p);
    if (p == 0) p = newnode(x);
    root = merge(merge(l, p), r);
}

void del(int x)
{
    int l, r, p;
    split(root, x, l, r);split(l, x - 1, l, p);
    root = merge(l, r);
}

int kth(int x, int k)
{
    if (tr[tr[x].ls].siz + 1 == k) return  x;
    if (tr[tr[x].ls].siz >= k) return kth(tr[x].ls, k);
    return kth(tr[x].rs, k - tr[tr[x].ls].siz - 1);
}

int getpre(int x)
{
    int l, r;
    split(root, x - 1, l, r);
    if (l == 0) return 0;
    int res = tr[kth(l, tr[l].siz)].key;
    root = merge(l, r);
    return res;
}

int getsuf(int x)
{
    int l, r;
    split(root, x, l, r);
    if (r == 0) return n + 1;
    int res = tr[kth(r, 1)].key;
    root = merge(l, r);
    return res;
}

void solve()
{
    cin >> n >> m;

    char op;int x;
    while (m--) {
        cin >> op;
        if (op == 'D') {
            cin >> x;
            vis[x] = 1;
            st[++tt] = x;
            insert(x);
        } else if (op == 'R') {
            x = st[tt--];
            vis[x] = 0;
            del(x);
        } else {
            cin >> x;
            if (vis[x] == 1) cout << 0 << '\n';
            else {
                int r = getsuf(x);
                int l = getpre(x);
                cout << r - l - 1 << '\n';
            }
        }
    }
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    srand(time(NULL));
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}